//
//  SEGVisitorAccessHistoryViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/4.
//

#import "SEGVisitorAccessHistoryViewModel.h"
#import "SEGVisitorAccessManager.h"

@implementation SEGVisitorAccessHistoryViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self seg_createCommand];
    }
    return self;
}
- (void)seg_createCommand {
    @weakify(self);
    self.command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            [[SEGVisitorAccessManager shareManager] requestVisitHistoryListWithPageNo:self.pageNo pageLength:10 finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    self.pageNo ++;
                    self.historyArray = [SEGVisitorAccessManager shareManager].historyArray;
                    [subscriber sendNext:self.historyArray];
                    [subscriber sendCompleted];
                } else {
                    NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            return nil;
        }];
    }];
}

@end
